import sys,time
from qasync import QEventLoop, asyncSlot
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, 
                            QPushButton, QLabel, QLineEdit, QGroupBox)
from PyQt5.QtCore import Qt
import asyncio
from tqsdk import TqApi, TqAuth, TqAccount, TqSim,TqKq
from PyQt5.QtGui import QIcon, QFont
from threading import Thread

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.api=None
        self.running=False
        self.main_layout = QVBoxLayout()
        
        # 设置窗口图标
        self.setWindowIcon(QIcon(r'E:\Private.Net\WorkShop\tqsdk\Tq\App.png'))
        
        # 主窗口样式
        self.setStyleSheet("""
            background-color: #f5f5f5;
            font-family: 'Microsoft YaHei';
        """)
        
        # 创建顶部标题
        title = QLabel("天勤量化交易系统")
        title.setAlignment(Qt.AlignCenter)
        title.setStyleSheet("""
            font-size: 24px;
            font-weight: bold;
            color: #333;
            padding: 20px;
        """)
        self.main_layout.addWidget(title)
        
        # 创建账户连接区域
        account_group = QGroupBox("账户连接")
        account_layout = QVBoxLayout()
        
        self.button = QPushButton("连接天勤账户")
        self._setup_button_style(self.button)
        account_layout.addWidget(self.button)
        
        account_group.setLayout(account_layout)
        self.main_layout.addWidget(account_group)
        
        # 创建交易功能区域
        trade_group = QGroupBox("交易功能")
        trade_layout = QHBoxLayout()
        
        self.button2 = QPushButton("查询账户持仓")
        self._setup_button_style(self.button2)
        trade_layout.addWidget(self.button2)
        
        trade_group.setLayout(trade_layout)
        self.main_layout.addWidget(trade_group)
        
        # 设置主布局
        self.setLayout(self.main_layout)
        
        # 连接信号槽
        self.button.clicked.connect(lambda: self.connect_tq())
        self.button2.clicked.connect(lambda: self.my_position())
        
        # 窗口大小
        self.resize(800, 600)
    
    def _setup_button_style(self, button):
        """统一设置按钮样式"""
        button.setStyleSheet("""
            QPushButton {
                background-color: #4CAF50;
                border: none;
                color: white;
                padding: 12px 24px;
                text-align: center;
                text-decoration: none;
                font-size: 16px;
                margin: 4px 2px;
                border-radius: 4px;
                min-width: 120px;
            }
            QPushButton:hover {
                background-color: #45a049;
            }
            QPushButton:pressed {
                background-color: #3e8e41;
            }
        """)

    @asyncSlot()
    async def connect_tq(self):
        """异步方法：连接天勤账户"""
        print("连接天勤账户...")
        tq_kq = TqKq()  # 创建快期模拟账户实例
        self.api = TqApi(account=tq_kq, auth=TqAuth("cps168", "alibaba999"))  # 使用账户和认证信息创建天勤API实例
        self.macc=self.api.get_account()  # 获取账户信息
        print("连接成功,账户可用资金为：",self.macc.available)  # 打印账户可用资金信息
        self.running=True
    
    @asyncSlot()
    async def my_position(self):
        async def query_pos():
            """异步方法：查询账户持仓"""
            if self.api:
                print("查询账户持仓...")
                # 修改为查询所有持仓而不是特定合约
                positions = self.api.get_position()  # 获取所有持仓
                async with self.api.register_update_notify(positions) as update_chan:
                    async for _ in update_chan:
                        while True:
                            # deadline = time.time() + 1
                            # self.api.wait_update(deadline=deadline)
                            for symbol, pos in positions.items():
                                print(f"时间：{time.strftime('%Y-%m-%d %H:%M:%S')}",
                                     f"{symbol} 持仓信息:", 
                                     f"多头:{pos.pos_long}", 
                                     f"空头:{pos.pos_short}")
        Thread(target=asyncio.run, args=(query_pos(),)).start()

    def closeEvent(self, event):
        """重写关闭事件，关闭前检查并关闭天勤API"""
        if self.running == True:
            self.running=False
        if self.api is not None:
            try:
                print("正在关闭天勤API连接...")
                #self.api.close()  # 关闭天勤API

                loop = asyncio.get_event_loop()
                loop.call_soon_threadsafe(self.api.close())
            except Exception as e:
                print("关闭天勤量化api出错",str(e))
            finally:
                pass
        event.accept()  # 接受关闭事件
    
    @asyncSlot()
    async def HelloWorld():
        print("Hello World!")
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setWindowTitle("天勤量化交易系统")
    window.resize(700,600)
    window.show()
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    sys.exit(app.exec())